package com.share.lottery.utils;

import java.lang.reflect.Field;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharePreferences的工具类
 * 
 * @author lmj
 * 
 */
public class PreferencesUtils {

	private Context mContext;

	public PreferencesUtils(Context mContext) {
		this.mContext = mContext;
	}

	/**
	 * 存储单个属性
	 * 
	 * @param field
	 * @param sp
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private void saveField(Field field, SharedPreferences sp, Object object)
			throws IllegalArgumentException, IllegalAccessException {
		field.setAccessible(true);
		Class fildType = field.getType();
		if (String.class == fildType || Character.class == fildType) {
			sp.edit()
					.putString(field.getName(),
							String.valueOf(field.get(object))).commit();
		} else if (Integer.TYPE == fildType || Integer.class == fildType) {
			sp.edit().putInt(field.getName(), field.getInt(object)).commit();
		} else if (boolean.class == fildType) {
			sp.edit().putBoolean(field.getName(), field.getBoolean(object))
					.commit();
		} else if (Long.class == fildType) {
			sp.edit().putLong(field.getName(), field.getLong(object)).commit();
		} else if (Float.class == fildType) {
			sp.edit().putFloat(field.getName(), field.getFloat(object))
					.commit();
		}

		// sp.edit().putString(field.getName(),
		// String.valueOf(field.get(object))).commit();
	}

	/**
	 * 拿到单个属性
	 * 
	 * @param field
	 * @param sp
	 * @return
	 */
	private String getFieldFromSp(Field field, SharedPreferences sp) {
		field.setAccessible(true);
		Class fildType = field.getType();
		if (String.class == fildType || Character.class == fildType) {
			return sp.getString(field.getName(), "");
		} else if (Integer.TYPE == fildType || Integer.class == fildType) {
			return String.valueOf(sp.getInt(field.getName(), 0));
		} else if (Boolean.class == fildType) {
			return String.valueOf(sp.getBoolean(field.getName(), false));
		} else if (Long.class == fildType) {
			return String.valueOf(sp.getLong(field.getName(), 0L));
		} else if (Float.class == fildType) {
			return String.valueOf(sp.getFloat(field.getName(), 0F));
		}
		return "";

	}

	/**
	 * 数据存储
	 * 
	 * @param sp
	 * @param classzz
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public void save(Object object) throws IllegalArgumentException,
			IllegalAccessException {
		SharedPreferences sp = mContext.getSharedPreferences(object.getClass()
				.getSimpleName().toLowerCase(), 2);// 1:read 2:write
		Field[] fields = object.getClass().getDeclaredFields();
		for (Field field : fields) {
			saveField(field, sp, object);
		}
	}

	/**
	 * 获得存储对象
	 * 
	 * @param classzz
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public Object getObjectFromSp(Class<?> classzz)
			throws InstantiationException, IllegalAccessException {
		Object object = classzz.newInstance();
		SharedPreferences sp = mContext.getSharedPreferences(
				classzz.getSimpleName(), 1);// 1:read 2:write
		Field[] fields = object.getClass().getDeclaredFields();
		for (Field field : fields) {
			try {
				FieldUtils.setValueToFiled(field, object,
						getFieldFromSp(field, sp));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return object;
	}
	
	

}

